package com.dementors.net.http.volley;

/**
 * Author  : itxuye(itxuye@gmail.com)|(http://itxuye.com)
 * Date    : 2015-12-06
 * Time    : 16:11
 * FIXME
 */

import android.app.ActivityManager;
import android.content.Context;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;


public class VolleyQueue {
  private static RequestQueue mRequestQueue;
  private static ImageLoader mImageLoader;

  private VolleyQueue() {

  }

  /**
   * 初始化我们的请求队列。这个地方有一个BitmapLruCache，这个是在后面做图片加载的时候会提到的图片缓存策略
   */
  public static void init(Context context) {
    mRequestQueue = Volley.newRequestQueue(context);

    int memClass = ((ActivityManager) context.getSystemService(
        Context.ACTIVITY_SERVICE)).getMemoryClass();
    // Use 1/8th of the available memory for this memory cache.
    int cacheSize = 1024 * 1024 * memClass / 8;
    mImageLoader = new ImageLoader(mRequestQueue, new BitmapLruCache(cacheSize));
  }

  public static RequestQueue getRequestQueue() {
    if (mRequestQueue != null) {
      return mRequestQueue;
    } else {
      throw new IllegalStateException("RequestQueue not initialized");
    }
  }

  public static ImageLoader getImageLoader() {
    if (mImageLoader != null) {
      return mImageLoader;
    } else {
      throw new IllegalStateException("ImageLoader not initialized");
    }
  }
}
